package com.songlanyun.modules.shop.controller.admin;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.applyareaagent.service.ApplyAreaAgentService;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.service.SendPrivateMessageMsgService;
import com.songlanyun.modules.shop.entity.ShopApplyEntity;
import com.songlanyun.modules.shop.model.dto.ShopApplyDTO;
import com.songlanyun.modules.shop.service.ShopApplyService;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 会员 -- admin
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-04 16:06:34
 */
@RestController
@RequestMapping("admin/shop/shopapply")
public class ShopApplyAdminController extends AbstractController {
    @Autowired
    private ShopApplyService shopApplyService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private SendPrivateMessageMsgService sendPrivateMessageMsgService;

    @Resource
    private ApplyAreaAgentService applyAreaAgentService;

    /**
     * 列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("account:shopApply:list")
    public R list(@RequestParam Map<String, Object> params) {
        //1、平台查看所有的申请
        if (isShop()) {
            //2、商家查看自己的申请
            //根据管理员账号id查询自己的会员id
            Boolean unionBusiness = MapUtil.getBool(params, "union_business");//是否是联盟商家
            Long userId = shopService.getUserIdByAdminId(getUserId(), ObjectUtil.isNull(unionBusiness) ? false : unionBusiness);//会员id
            params.put("user_id", userId);
        }

        //设置区县代理参数
        if (getAreaAgentId() != 0L) {
            applyAreaAgentService.setAreaAgentParams(getAreaAgentId(), params);
        }

        PageUtils page = shopApplyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("account:shopApply:info")
    public R info(@PathVariable("id") Long id) {
        ShopApplyEntity shopApply = shopApplyService.getById(id);

        return R.ok().put("shopApply", shopApply);
    }


    /**
     * 删除
     */
    @SysLog("删除商家审核记录")
    @DeleteMapping("/delete")
//    @RequiresPermissions("account:shopApply:delete")
    public R delete(@RequestBody Long[] ids) {
        verifyAdmin();//平台才能操作
        shopApplyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 审核会员申请商家
     */
    @SysLog("审核会员申请商家")
    @RepeatSubmit("3")
    @PostMapping("/audit")
//    @RequiresPermissions("account:shopApply:update")
    public R audit(@RequestBody ShopApplyDTO dto) {
        if (dto.getId() == null || dto.getAuditStatus() == null) {
            throw new RRException(MemberException.INCOMPLETE_PARAMETER);
        }
        ShopApplyEntity audit = shopApplyService.audit(dto);

        //发送互动消息
        sendPrivateMessageMsgService.send(new MessageTO(PrivateMessageConstant.MessageType.INTERACTION_MESSAGE,
                PrivateMessageConstant.ModelType.CHECK, audit.getId(),
                audit.getUnionBusiness() ? SysConstant.InteractionType.UNION_BUSINESS_CHECK : SysConstant.InteractionType.SHOP_CHECK,
                audit.getUserId()));

        return R.ok();
    }


}
